import { IBaseTable } from '@feidao/web/atom/nodejs/db/interfaces';
import query from '@feidao/web/atom/nodejs/db/query';
import format from '@feidao/web/atom/nodejs/dt/format';
import get_user_by_usercode from '@feidao/web/atom/nodejs/msg/get-user-by-usercode';
import { IncomingHttpHeaders } from 'http';
import { systemid } from './config';




declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	filter: {};
	sort: {};
	page_size: number;
	page_no: number;
}

interface data extends IBaseTable {
	_id: string;
	equip_no: string;
	Date: string;
	state: string;
	equip_location: string;
	enabled: string;
	equip_name: string;
	admin_phone: string;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	const { exec, prepare } = query(action_id, session_id, spaceid, systemid);
	prepare('equipment', [
		'_id',
		'equip_no',
		'equip_location',
		'enabled',
		'equip_name',
		'admin_phone'
	], msg.filter);
	const [[data]] = await exec();
	const d = data as data;
	if (!d.enabled) {
		d.enabled = '启用';
	} else {
		d.enabled = '禁用';
	}

	const equip_no = d.equip_no;
	const state = await get_user_by_usercode(action_id, session_id, "self_sell", "self_sell", equip_no);
	if (state.length > 0) {
		const time = state[state.length - 1].lastReplyTime;
		d.Date = format(time, 'yyyy-mm-dd HH:MM:ss');
		d.state = "在线";
	} else {
		d.Date = "";
		d.state = "离线";
	}
	return d;
}
